#!/bin/bash
# author:fsz

# 脚本命令执行报错退出显示
set -e
    
str=$1

############版本
str1=master
str2=OpenHarmony-3.2-beta2
str3=OpenHarmony-3.2-beta2-merge
str3_1=OpenHarmony-3.2-beta2
str4=OpenHarmony-3.2-release

###########变量
url=https://gitee.com/openharmony/manifest.git
edi=""
edi2=""
xml=""

##############
function main(){
if [ ! "$str" ]; then
    show_mu
else
    check 
fi
}

# 创建并进入版本目录
function make_dir() {
if [ ! -e "$str" ]; then
    mkdir "$str"
    cd "$str"
elif [ ! -e "$str"_temp ]; then
    mkdir "$str"_temp
    cd "$str"_temp
else
    rm -rf "$str"_temp
    mkdir "$str"_temp
    cd "$str"_temp
fi
}
####################判断
function check (){
if [ "${str}" == "${str1}"  ]; then
    xml="default.xml"
    edi=${str1}
    edi2=${str1} 
elif [ "${str}" == "${str2}" ]; then
    xml="default.xml"
    edi=${str2}
    edi2=${str2}
elif [ "${str}" == "${str3}" ]; then
    xml="thead_develop.xml"
    edi=${str3}
    edi2=${str3_1}
elif [ "${str}" == "${str4}" ]; then
    xml="default.xml"
    edi=${str4}
    edi2=${str4}
else 
    echo -e "\nproduct is not in product list, please recheck!!!"
    show_mu 
    return 0
fi
make_dir
download
download_infor
}

################目录
function show_mu(){
echo -e "*******************************************"
echo -e "* Welcome to download OpenHarmony Code    *"
echo -e "* Please Choice OpenHarmony SDK           *"
echo -e "* OpenHarmony Version                     *"
echo -e "*   OpenHarmony master          prese 1.1 *"
echo -e "*   OpenHarmony-3.2-beta2       press 1.2 *"
echo -e "*   OpenHarmony-3.2-beta2-merge press 1.3 *"
echo -e "*   OpenHarmony-3.2-release     press 1.4 *"
echo -e "*******************************************"
echo -e "请输入你想下载的版本："
read -r line
str=$line 
check 
}


################下载
function download(){
#repo init -u $url -b $edi -m $xml --no-repo-verify
#repo sync -c
#repo forall -c 'git lfs pull'
#repo start $edi2 --all
echo ".........."
echo ".........."
echo ".........."
}


####################打印下载信息
function download_infor(){
echo -e "you have been download OpenHarmony $edi code"
echo -e "url       : $url"
echo -e "branch    : $edi"
echo -e "xml_name  : $xml"
echo -e "code_path : /home/wen_fei/OpenHarmony/OpenHarmony-3.2-Release"
echo -e "init   cmd: repo init -u $url -b $edi -m $xml --no-repo-verify"
echo -e "sync   cmd: repo sync -c"
echo -e "lfs    cmd: repo forall -c 'git lfs pull'"
echo -e "set_br cmd: repo start $edi2 --all"
}

main 

